package com.yiwei.hdb.view;

import android.app.*;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.UI;
import com.yiwei.hdb.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 17-11-22.
 */

public class SendCodeDialog extends android.app.Dialog {
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.left_button)
    TextView leftButton;
    @BindView(R.id.d_line)
    View dLine;
    @BindView(R.id.right_button)
    TextView rightButton;

    public SendCodeDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.change_phone_code_dialog, null);
        setContentView(view);
        setCancelable(false);
        ButterKnife.bind(this, view);


        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(getContext()).widthPixels / 10 * 8;
        mWindow.setAttributes(lp);
    }

    public void setDialogClickListener(DialogDefaultClickListener dialogDefaultClickListener) {
        this.dialogDefaultClickListener = dialogDefaultClickListener;
    }

    private DialogDefaultClickListener dialogDefaultClickListener;

    @OnClick({R.id.btn_send_code, R.id.left_button, R.id.right_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_send_code:
                dialogDefaultClickListener.sendCode();
                break;
            case R.id.left_button:
                dialogDefaultClickListener.cancel();
                break;
            case R.id.right_button:
                if (TextUtils.isEmpty(UI.toString(editCode))) {
                    ToastUtil.makeText(getContext(), "验证码不能为空");
                    return;
                }
                dialogDefaultClickListener.confirm(UI.toString(editCode));
                break;
        }
    }

    public void setCodeText(String text, boolean enable) {
        btnSendCode.setText(text);
        btnSendCode.setEnabled(enable);
    }

    public void setContent(String phone) {
        phone = Util.replaceString(phone, 5, 4, '*');
        txtContent.setText("请输入" + phone + "收到的验证码");
    }

    public interface DialogDefaultClickListener {
        void confirm(String code);

        void cancel();

        void sendCode();
    }


}
